package com.darrenchan.spark.sql

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

object DataFrameWordCount {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().master("local[2]").appName("DataFrameWordCount").getOrCreate()

    val helloRDD = spark.sparkContext.textFile("hello.txt")

    import spark.implicits._
    val helloDF = helloRDD.flatMap(_.split(" ")).map(word => Word(word)).toDF()

    //方法1
    helloDF.createOrReplaceTempView("wordtable")
    spark.sql("select word, count(*) as total from wordtable group by word").show() //加不加as都行
    //方法2
    helloDF.groupBy("word").count().withColumnRenamed("count", "total").show()
    //方法3，这种写法必须
    //import org.apache.spark.sql.functions._
    helloDF.groupBy("word").agg(count("*").as("total")).show()

    spark.stop()
  }
}

case class Word(word: String)
